package com.zjsru.plan2023.oneday;

/**
 * @Author: CookLee
 * @Date: 2023/5/3
 *
 * 检查替换后的词是否有效
 *
 * 输入：s = "aabcbc"
 * 输出：true
 * 解释：
 * "" -> "abc" -> "aabcbc"
 * 因此，"aabcbc" 有效。
 *
 *
 * 输入：s = "abcabcababcc"
 * 输出：true
 * 解释：
 * "" -> "abc" -> "abcabc" -> "abcabcabc" -> "abcabcababcc"
 * 因此，"abcabcababcc" 有效。
 */
public class IsValid {
    
    private final String DEFAULT_EN_PREFIX = "abc";
    
    /**
     * 栈
     *
     * @param s string
     * @return boolean
     */
    public boolean isValid(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            sb.append(c);
            //模拟栈顶的 3 个元素
            if (sb.length() >= 3 && sb.substring(sb.length() - 3).contains(DEFAULT_EN_PREFIX)) {
                //满足插入的字符为abc即删除
                sb.delete(sb.length() - 3, s.length());
            }
        }
        return String.valueOf(sb).isEmpty();
    }
    
    public static void main(String[] args) {
        IsValid isValid = new IsValid();
        String s = "aabcbc";
        System.out.println(isValid.isValid(s));
    }
}
